package hoang.hut.app.ui;

import hoang.hut.app.screen.dialog.PositiveButtonDialog;
import hoang.hut.libs.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class BaseActivity extends Activity{
	
	protected ProgressDialog progressDialog;
	
    /**
     * Show custom dialog with only positive button
     * 
     * @param titleId
     * @param iconId
     * @param messageId
     * @param positiveBtnId
     */
    public void showCustomPositiveDialog(Context context, int titleId, int messageId, int positiveBtnId)
    {
        PositiveButtonDialog dialog = new PositiveButtonDialog(context, titleId, messageId, positiveBtnId);
        
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    /**
     * Open progress dialog
     * 
     * @param text
     */
    public void showProgressDialog(String text)
    {
        if (progressDialog == null)
        {
            progressDialog = ProgressDialog.show(BaseActivity.this,
                null, text, true);
            progressDialog.setCancelable(false);
        }
    }

    /**
     * Open progress dialog
     */
    public void showProgressDialog()
    {
        showProgressDialog(getString(R.string.message_please_wait));
    }

    /**
     * Close progress dialog
     */
    public void closeProgressDialog()
    {
        if (progressDialog != null)
        {
            progressDialog.cancel();
            progressDialog = null;
        }
    }
    
    // ======================= KEYBOARD MANAGER =======================

    /**
     * Show soft keyboard
     * 
     * @param editText
     */
    public void showKeyboard(EditText editText)
    {
        InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null)
        {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                0);
        }
    }

    /**
     * Hide soft keyboard
     * 
     * @param editText
     */
    public void hideKeyboard(EditText editText)
    {
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
            editText.getWindowToken(), 0);
    }

}
